﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GS.CMS.Entities;

public partial class usercontrols_menu : System.Web.UI.UserControl
{
    DataTable dtMenu;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadMenu();

    }
    public DataTable Menu
    {
        set
        {
            dtMenu = value;
        }
    }
    private void LoadMenu()
    {
        string html = "", strHotDealsActive="",strNewProductsActive="", strCat="", strHomeActive="", strCategoryActice="";
        try
        {
            if (dtMenu.Rows.Count > 0)
            {
                DataView dvRoot = null, dvSub1 = null, dvSub2 = null;
                DataTable dtRoot = null, dtSub1 = null, dtSub2 = null;
                //Check page
                string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
                System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
                string sRet = oInfo.Name;
                //CheckLogin(sRet);
                strCat = Request.QueryString["root"];
                if (sRet == "products-list.aspx")
                {
                    if (strCat == "new")
                        strNewProductsActive = "active";
                    else if (strCat == "hotdeals")
                        strHotDealsActive = "active";
                }
                else if (sRet != "category-list.aspx")
                {
                    strHomeActive = "active";
                }
                html += string.Format(@"<ul id='menusys_mega'>
                                <li class='first-item {0}'><a class=' {0}' title='Home' href='default.aspx'>
                                    <span class='no-image'><span class='menu-title'>HOME</span></span></a></li>
                                <li class='{1}'><a class=' item {1}' title='new products' href='products-list.aspx?root=new'>
                                    <span class='no-image'><span class='menu-title'>NEW PRODUCTS</span></span></a>", strHomeActive, strNewProductsActive);

                html += string.Format(@"<li class='{0}'><a class=' item {0}' title='hot deals' href='products-list.aspx?root=hotdeals'>
                                    <span class='no-image'><span class='menu-title'>HOT DEALS</span></span></a></li>", strHotDealsActive);
                //Lọc lấy danh sách Category Root
                dvRoot = dtMenu.DefaultView;
                dvRoot.RowFilter = "ParentCategoryID Is Null OR ParentCategoryID =''";
                dtRoot = dvRoot.ToTable();
                for (int i = 0; i < dtRoot.Rows.Count; i++)
                {
                    if (sRet == "category-list.aspx")
                    {
                        if (dtRoot.Rows[i]["ParentCategoryID"] == DBNull.Value && strCat == (string)dtRoot.Rows[i]["CategoryID"])
                            strCategoryActice = "active";
                        else
                            strCategoryActice = "";
                    }
                    //Lọc lấy danh sách Category Sub1
                    dvSub1 = dtMenu.DefaultView;
                    dvSub1.RowFilter = "ParentCategoryID ='" + dtRoot.Rows[i]["CategoryID"] + "'";
                    dtSub1 = dvSub1.ToTable();

                    if (dtSub1.Rows.Count > 0)
                    {

                        html += string.Format(@"<li id='menu-{2}' class='hasChild {3}' ><a id='menusys{2}'  class=' item {3}' title='{0}' href='category-list.aspx?root={1}'>
                                    <span class='no-image'><span class='menu-title'>{0}</span></span></a>", dtRoot.Rows[i]["Name"] + "", dtRoot.Rows[i]["CategoryID"] + "", i, strCategoryActice);
                        //Thêm sub1
                        html += string.Format(@"<div id='menu-{0}_megamenu' class='menusub_mega'>
                                                    <div class='submenu-wrap'>", i);
                        for (int k = 0; k < dtSub1.Rows.Count; k++)
                        {
                            //Kiểm tra sub 2
                            dvSub2 = dtMenu.DefaultView;
                            dvSub2.RowFilter = "ParentCategoryID ='" + dtSub1.Rows[k]["CategoryID"] + "'";
                            dtSub2 = dvSub2.ToTable();
                            html += string.Format(@"<div class='megacol column{0}'>
                                                                <div class='mega-group'>
                                                                    <a id='menusys27' class=' item' href='category-list.aspx?root={1}&subcat={2}'><span class='no-image'>
                                                                        <span class='menu-title'>{3}</span><span class='menu-desc'></span></span></a></div>",
                                                      k, dtSub1.Rows[k]["RootID"] + "", dtSub1.Rows[k]["CategoryID"] + "", dtSub1.Rows[k]["Name"] + "");
                            //Thêm sub 2
                            html += string.Format(@"<div class='submenu-wrap'>
                                        <div class='megacol column{0}'>
                                        <ul class='mega-ul ul'>", k);
                            for (int l = 0; l < dtSub2.Rows.Count; l++)
                            {
                                html += string.Format(@"<li class='mega-li'><a class=' item' href='category-list.aspx?root={0}&subcat={1}&subcat0={2}'>
                                                        <span class='no-image'><span class='menu-title'>{3}</span></span></a></li>
                                                         ", dtSub2.Rows[l]["RootID"] + "", dtSub2.Rows[l]["ParentCategoryID"] + "", dtSub2.Rows[l]["CategoryID"] + "", dtSub2.Rows[l]["Name"] + "");

                            }
                            html += "</ul></div></div>";
                            //Kết thúc menu sub 2                                                                  
                            html += "</div>";
                            //kết thúc menu sub 1
                        }
                        html += "</div></div></li>";
                        //Kết thúc menu root
                    }
                    else
                        html += string.Format(@"<li class='{2}'><a class=' item {2}' title='{0}' href='category-list.aspx?root={1}'>
                                    <span class='no-image'><span class='menu-title'>{0}</span></span></a></li>", dtRoot.Rows[i]["Name"] + "", dtRoot.Rows[i]["CategoryID"] + "", strCategoryActice);
                }
                html += "</ul>";
                ltrMenu.Text = html;
            }
        }
        catch (Exception ex)
        {
            TrackingError.AddTrackingError("Author: sanghv - Funtion: LoadMenu - Usercontrol:menu-horizontal", ex.Message);
        }
    }
}